//
//  HJAvatorVC.m
//  project
//
//  Created by jc on 2019/5/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJAvatorVC.h"

@interface HJAvatorVC ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UIButton *changeAvatorBtn;

@end

@implementation HJAvatorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"头像";
    [self.view setupShadowWithView:self.bgView];
    [self.headImage rounded:UIRectCornerAllCorners radius:self.headImage.width/2 borderColor:[UIColor blackColor] borderWidth:1];
    [self.view setupShadowWithView:self.changeAvatorBtn];
    
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:[HJUserManager getUserModel].avatar]];
    @weakify(self)
    [[self.changeAvatorBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [TZImagePickerTool selectImageForEditFrom:self complete:^(UIImage *origionImg, UIImage *editedImage) {
            self.headImage.image = editedImage;
            [self uploadHeadImageWithImage:editedImage bgImage:origionImg];
        } isCamera:NO];
    }];
}

// 上传头像
-(void)uploadHeadImageWithImage:(UIImage *)image bgImage:(UIImage *)bgImage {
    @weakify(self)
    NSString * url = [Api stringByAppendingString:UploadImageApi];
    [HJHttpTool uploadImageWithUrl:url params:nil name:@"multipartFiles" imageArray:@[image,bgImage] completion:^(NSArray *result, NSError *error) {
        @strongify(self)
        if (result && result.count >=1) {
            [HJUserManager updateUserModelWithParams:@{
                                                        @"avatar":[NSString stringWithFormat:@"%@",result[0]],
                                                        @"avatarPic": [NSString stringWithFormat:@"%@",result[1]]
                                                        } success:^{
                // 更新融云用户头像
                [mNotificationCenter postNotificationName:NotificationRCUserInfo object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    }];
}
@end
